package com.sos.service;

import java.util.List;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

//import com.google.android.maps.GeoPoint;
import com.sos.util.HttpUtil;
import com.sos.vo.SetInfo;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
/****
 * 
 * 系统位置服务类
 * 
 * 参考文献：
 * Location and Maps
 * http://developer.android.com/guide/topics/location/index.html
 * **/
public class LactionService {
	private final static String TAG = LactionService.class.getSimpleName();
	private final static String URL="http://maps.googleapis.com/maps/api/geocode/json?region=cn&latlng=";
	private Context context;
	private String loaction="";//文字地标
	private Location locat;
	 Location loca;
	 SetInfo set=null;
	 WWWService www=null;
	public LactionService(Context context) {
		Log.d(TAG, "LactionService");
		this.context = context;
	}
	LocationListener GPS_listener =new LocationListener(){

		/***
		 * 位置改变监听器
		 * **/
		@Override
		public void onLocationChanged(Location loc) {
			// TODO Auto-generated method stub
			Log.d(TAG, "onLocationChanged");
			  Log.d(TAG, ""+loc.getLatitude()+loc.getLongitude());
			  loca=loc;
			   www=new WWWService(context);
			  SetManager setm=new SetManager(context);
			   set=setm.get();
			  new Thread(){
				  public void run(){
					  www.loaction(loca.getLatitude()+"", loca.getLongitude()+"", set.getWww_ip(), set.getWww_port());
				  }
			  }.start();
			 
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			//Log.d(TAG, "onProviderDisabled");
		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			//Log.d(TAG, "onProviderEnabled");
		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub
			//Log.d(TAG, "onStatusChanged");
		}
		
	};
	//获取坐标
	public String getLocation() {
		Log.d(TAG, "getLocation");
		String l = "";
		LocationManager locMan = (LocationManager) context  
	                .getSystemService(Context.LOCATION_SERVICE);  
		 
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, GPS_listener);
		locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, GPS_listener);
		locat= locMan  
	                .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
	     if (locat == null) {  
	        	Log.d(TAG, "NETWORK_PROVIDER");
	        	locat = locMan  
	                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
	            if(locat != null)
	            {
	            	loaction=locat.getLatitude()+","+locat.getLongitude();
	  	              Log.d(TAG, ""+locat.getLatitude()+locat.getLongitude());
	            }
	          
	     }
	     else 
	     {
	    	 loaction="暂时无法获取位置";
	     }  

		return loaction;
	}
	//获取坐标
	public Location getLoca() {
		Log.d(TAG, "getLoca ");
		LocationManager locMan = (LocationManager) context  
	                .getSystemService(Context.LOCATION_SERVICE);  
		 
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, GPS_listener);
		locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, GPS_listener);
	    Location location = locMan  
	                .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
	     if (location == null) {  
	        	Log.d(TAG, "NETWORK_PROVIDER");
	            location = locMan  
	                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
	            if(location != null)
	            {
	            	 
	  	              Log.d(TAG, ""+location.getLatitude()+location.getLongitude());
	            }
	          
	     }
	     else 
	     {
	    	 
	    	 Log.d(TAG, "暂时无法获取位置");
	     }  

		return location;
	}

	/***
	 * 
	 * 解析坐标详细地址
	 * **/
    public String getAddress(String location){
    	Log.d(TAG, "getAddress");
    	location=HttpUtil.httpRequestInfo(URL+location+"&sensor=false&language=zh-cn");
    	try {
			JSONObject json=new JSONObject(location);
			//Log.d(TAG, json.getJSONArray("results").toString());
			if(json.getString("status").equals("OK"))
			{
				Log.d(TAG, "OK,"+location);
				location=json.getJSONArray("results").getJSONObject(0).getString("formatted_address");
				
			}
			else 
			{
				Log.d(TAG, "NO OK====,"+json.getString("status"));
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//Log.d(TAG, location);
		return location;
    	
    }
}
